<?php

require_once('libraryfiles/config.php');
$return_url = @ $_GET["return_url"];
if (isset($_POST['btnLogin'])) {
    $errormsg = '';
    $username = $_POST['email_add'];
    $pwd = $_POST['passwd'];
    $genPass = GenPassword($pwd);
    $account_type = $_POST['accounttype'];
    $where_user = array(
        Tbluser::email => $username,
        Tbluser::user_pass => $genPass,
        Tbluser::user_type_id => $account_type,
    	Tbluser::status => 1,
    );
    $user_check = select(Tbluser::Tbluser, array(Tbluser::email, Tbluser::user_pass, Tbluser::status, Tbluser::first_name, Tbluser::last_name, Tbluser::account_type_id, Tbluser::user_type_id, Tbluser::perm, Tbluser::id,Tbluser::state_id, Tbluser::location, Tbluser::country_id), $where_user);
    $row = mysql_fetch_assoc($user_check);
    if (dbNumRows($user_check) == 0) {
        header('Location: login.php?error=USER_NOT_EXIST');
    } else {
        if ($row['status'] == 0) {
            header('Location: login.php?error=USER_NOT_ACTIVATE_YET');
            exit();
        }
        $_SESSION['username'] = $username;
        $_SESSION['fullname'] = $row['first_name'] . ' ' . $row['last_name'];
        $_SESSION['acc_id'] = $row['id'];
        $_SESSION['password'] = $pwd;
		$_SESSION['province_id'] = $row[Tbluser::state_id];
		$_SESSION['disctict_id'] = $row[Tbluser::location];
		$_SESSION['usertype'] = $row['user_type_id'];
		$_SESSION['acc_type'] = $row['account_type_id'];
		$_SESSION['perm'] = $row['perm'];
		$_SESSION['orderSesion'] = session_id();
        $SET = array(
            Tbluser::last_visit => date('Y-m-d h:i:s'),
            Tbluser::acc_online => 1,
            Tbluser::session_id => session_id(),
        );
        $where_log = array(Tbluser::id => $row['id']);
        $result_1s = update(Tbluser::Tbluser, $SET, $where_log);
        if (isset($_POST['chkRemember'])) {
            setcookie("accounttype", $account_type, time() + 3600 * 24);
            setcookie("username", $username, time() + 3600 * 24);
            setcookie("password", $pwd, time() + 3600 * 24);
        } else {
            setcookie("username", "");
            setcookie("password", "");
            setcookie("accounttype", "");
        }
        if ($account_type == 2) {   ///////////////////////////////changed
            //header("location:" . W_ROOT . "/company/dashboard");
        	header("location: ".base_url."account/");
        } else if ($account_type == 3) {
            //header("location:" . $return_url);
        	header("location:index.php");
        } else {
            header("location: ".base_url."account/");
        }
    }
} else {


    $errormsg = '';
    $username = $_SESSION['username'];
    $pwd0 = $_SESSION['pass'];
    $pwd = $_SESSION['pass'];
    $account_type = $_SESSION['usertype'];
    $sql = "SELECT * FROM tbluser WHERE email= '" . $username . "' AND user_pass = '" . $pwd . "' AND user_type_id =" . $account_type . "";
    $result = mysql_query($sql);
    //print_r($result);die();
    $row = mysql_fetch_assoc($result);

    //echo $user;
    if (mysql_num_rows($result) == 0) {
        header('Location: login.php?error=1');
        exit();
    } else {
        if ($row['status'] == 0) {
            header('Location: login.php?error=2');
            exit();
        }
        $_SESSION['username'] = $username;
        $_SESSION['fullname'] = $row['first_name'] . ' ' . $row['last_name'];
        $_SESSION['acc_id'] = $row['id'];
        $_SESSION['password'] = $pwd;
        $_SESSION['password0'] = $pwd0;
        $_SESSION['usertype'] = $row['user_type_id'];
        $_SESSION['acc_type'] = $row['account_type_id'];
        $_SESSION['perm'] = $row['perm'];
        $_SESSION['orderSesion'] = session_id();
        $sql_1 = "UPDATE tbluser SET last_visit = NOW(), acc_online='1', session_id = '" . session_id() . "' WHERE id = " . $row['id'];
        $result_1 = mysql_query($sql_1);


        if (isset($_POST['remember'])) {
            setcookie("cookname", $_SESSION['username'], time() + 60, "/");
            setcookie("cookpass", $_SESSION['password'], time() + 60, "/");
            setcookie("cookpass0", $_SESSION['password0'], time() + 60, "/");
        } else {
            setcookie("cookname", "", time() - 60, "/");
            setcookie("cookpass", "", time() - 60, "/");
            setcookie("cookpass0", "", time() - 60, "/");
        }

        if ($account_type == 2) {
            //header("location:" . W_ROOT . "/company/dashboard/"); ////////////////////////////////changed
        	header("location: ".base_url."account/");
        } else if ($account_type == 3) {
            //header("location:" . $return_url);
        	header("location: ".base_url."account/");
        } else {
            header("location:index.php");
        }
    }
}